const sdk = require('./sdk');

module.exports.get = async function (data) {
  // console.log(`execute branches.get : ${JSON.stringify(data)}`);
  const { query } = data.data;
  // 对项目id编码
  // data.data.query.project_id  = encodeURIComponent(data.data.query.project_id);
  data.data.json = {
    DepotPath: query.project_id,
    KeyWord: query.search,
    PageNumber: query.page,
    PageSize: query.per_page,
    Action: 'DescribeGitBranches',
  };
  const tempRes = await sdk.request('/DescribeGitBranches', 'POST', data);
  const resp = tempRes?.Response?.Branches?.map((one) => {
    // {
    //   IsDefaultBranch: false,
    //   Sha: 'd4cebf6ed5e78ec39172d6999ffa1bd38344eb39',
    //   IsReadOnly: false,
    //   DenyForcePush: false,
    //   ForceSquash: false,
    //   SpecBranchType: '',
    //   LastCommitDate: 1731496819000
    // },
    // [
    //   {
    //     protected: true,
    //     developers_can_push: true,
    //     developers_can_merge: false,
    //     name: 'master',
    //     commit: {
    //       id: 'b226eb1ed8cb15be5ff59775c7eacab899f54400',
    //       message: '1\r\n2',
    //       parent_ids: [Array],
    //       authored_date: '2024-11-13T09:51:27+0000',
    //       author_name: 'lisensen',
    //       author_email: '38fb667f68f74026908b1447e2ebb06c@git.tencent.com',
    //       committed_date: '2024-11-13T09:51:27+0000',
    //       committer_name: 'lisensen',
    //       committer_email: '38fb667f68f74026908b1447e2ebb06c@git.tencent.com',
    //       title: '1 2',
    //       scroll_object_id: null,
    //       created_at: '2024-11-13T09:51:27+0000',
    //       short_id: 'b226eb1e'
    //     },
    //     description: '',
    //     branch_type: { name: 'Mainline', color: '#CC0033' }
    //   },
    // ]
    const obj = {
      protected: one.IsProtected,
      name: one.BranchName,
    };
    return obj;
  });
  console.log('----------查询到的仓库的分支的长度: ', resp?.length);
  return resp;
};
module.exports.post = async function (data) {
  // console.log(`execute branches.post : ${JSON.stringify(data)}`);
  // 对项目id编码
  const { json } = data.data;
  data.data.json = {
    BranchName: json.branch_name,
    DepotId: json.project_id,
    Note: '',
    StartPoint: json.ref,
    Action: 'CreateGitBranch',
  };
  // data.data.query.project_id  = encodeURIComponent(data.data.query.project_id);
  return  await sdk.request('/CreateGitBranch', 'POST', data);
};
